package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.mapper.PayableDetailMapper;
import com.intelligent.mapper.PurchaseOrderMapper;
import com.intelligent.mapper.PurchaseSupplierMapper;
import com.intelligent.pojo.PayableDetail;
import com.intelligent.pojo.PurchaseOrder;
import com.intelligent.pojo.PurchaseSupplier;
import com.intelligent.service.PayableDetailService;
import com.intelligent.vo.PayableDetailPageReq;
import com.intelligent.vo.PayableDetailResp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

@Service
public class PayableDetailServiceImpl extends ServiceImpl<PayableDetailMapper, PayableDetail>
    implements PayableDetailService {

    @Resource
    private PurchaseSupplierMapper purchaseSupplierMapper;

    @Resource
    private PurchaseOrderMapper purchaseOrderMapper;

    @Override
    public Page<PayableDetailResp> page(PayableDetailPageReq req) {
        Page<PayableDetail> entityPage = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<PayableDetail> qw = new LambdaQueryWrapper<>();
        if (req.getSupplierId() != null) qw.eq(PayableDetail::getSupplierId, req.getSupplierId());
        if (req.getPurchaseOrderId() != null) qw.eq(PayableDetail::getPurchaseOrderId, req.getPurchaseOrderId());
        if (req.getReconciliationId() != null) qw.eq(PayableDetail::getReconciliationId, req.getReconciliationId());
        if (req.getPayableType() != null) qw.eq(PayableDetail::getPayableType, req.getPayableType());
        if (req.getStatus() != null) qw.eq(PayableDetail::getStatus, req.getStatus());
        if (req.getSettlementType() != null) qw.eq(PayableDetail::getSettlementType, req.getSettlementType());

        qw.orderByDesc(PayableDetail::getId);
        this.page(entityPage, qw);

        Page<PayableDetailResp> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        voPage.setRecords(entityPage.getRecords().stream().map(this::toResp).collect(Collectors.toList()));
        return voPage;
    }

    @Override
    public PayableDetailResp detail(Long id) {
        PayableDetail d = this.getById(id);
        return d == null ? null : toResp(d);
    }

    public boolean create(PayableDetail detail) {
        if (detail.getPayableAmount() != null && detail.getPaidAmount() != null) {
            detail.setUnpaidAmount(detail.getPayableAmount().subtract(detail.getPaidAmount()));
        }
        return this.save(detail);
    }

    private PayableDetailResp toResp(PayableDetail d) {
        PayableDetailResp r = new PayableDetailResp();
        r.setId(d.getId());

        r.setSupplierId(d.getSupplierId());
        r.setPurchaseOrderId(d.getPurchaseOrderId());
        r.setReconciliationId(d.getReconciliationId());
        r.setPayableType(d.getPayableType());
        r.setPayableAmount(d.getPayableAmount());
        r.setPaidAmount(d.getPaidAmount());
        r.setUnpaidAmount(d.getUnpaidAmount());
        r.setDueDate(d.getDueDate());
        r.setStatus(d.getStatus());
        r.setPaymentTerm(d.getPaymentTerm());
        r.setSettlementType(d.getSettlementType());
        r.setRemark(d.getRemark());
        r.setCreateTime(d.getCreateTime());

        if (d.getSupplierId() != null) {
            PurchaseSupplier s = purchaseSupplierMapper.selectById(d.getSupplierId());
            if (s != null) r.setSupplierName(s.getSupplierName());
        }
        if (d.getPurchaseOrderId() != null) {
            PurchaseOrder po = purchaseOrderMapper.selectById(d.getPurchaseOrderId());
            if (po != null) r.setPurchaseOrderNo(po.getOrderNo());
        }
        return r;
    }
}




